package com.ksm.client.document.view;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.DateCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SafeUri;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.ResizeComposite;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Singleton;
import com.ksm.client.document.DocumentProperties;
import com.ksm.client.document.presenter.DocumentGridPresenter.IDocumentGridView;
import com.ksm.client.document.presenter.IDocumentGridPresenter;
import com.ksm.transfer.dto.DocumentDTO;
import com.sencha.gxt.core.client.XTemplates;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.event.CellClickEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.form.NumberPropertyEditor;
import com.sencha.gxt.widget.core.client.form.NumberPropertyEditor.DoublePropertyEditor;
import com.sencha.gxt.widget.core.client.grid.ColumnConfig;
import com.sencha.gxt.widget.core.client.grid.ColumnModel;
import com.sencha.gxt.widget.core.client.grid.Grid;
import com.sencha.gxt.widget.core.client.grid.GridView;

@Singleton
public class DocumentGridView extends ResizeComposite implements IDocumentGridView, IsWidget {

	interface ImagesCellTemplates extends XTemplates {

		@XTemplate("<a href=\"{name} \"><img width=\"22\" height=\"22\" src=\"{imageUri}\"/></a>")
		SafeHtml imageLink(SafeUri imageUri, String name);

	}

	interface PdfLinkCellTemplates extends XTemplates {

		@XTemplate("<a href=\"{fileLink} \"><img width=\"22\" height=\"22\" src=\"images/logo-pdf.jpg\"/></a>")
		SafeHtml pdfLink(String fileLink);

	}

	private IDocumentGridPresenter presenter;

	private static DocumentUiBinder uiBinder = GWT.create(DocumentUiBinder.class);

	interface DocumentUiBinder extends UiBinder<Widget, DocumentGridView> {
	}

	private final DocumentProperties documentProperties = GWT.create(DocumentProperties.class);

	@UiField(provided = true)
	final NumberFormat numberFormat = NumberFormat.getFormat("0.00");
	@UiField(provided = true)
	NumberPropertyEditor<Double> doublePropertyEditor = new DoublePropertyEditor();

	@UiField
	Grid<DocumentDTO> documentGrid;
	@UiField
	GridView<DocumentDTO> documentGridView;
	@UiField
	ColumnModel<DocumentDTO> documentColumnModel;
	@UiField
	ListStore<DocumentDTO> documentStore;

	@UiField
	TextButton newDocumentBtn;

	@UiFactory
	ColumnModel<DocumentDTO> createDocumentColumnModel() {
		return documentColumnModel;
	}

	@UiFactory
	ListStore<DocumentDTO> createListStore() {
		return documentStore;
	}

	@Override
	public void setListStore(ListStore<DocumentDTO> listeStore) {
		this.documentStore = listeStore;
		documentGrid.reconfigure(this.documentStore, documentColumnModel);
	}

	public void addToStore(DocumentDTO dto) {
		this.documentStore.add(0, dto);
		documentGrid.reconfigure(this.documentStore, documentColumnModel);
	}

	private Widget uiWidget = null;

	private void initializeColumnModel() {
		// ID
		ColumnConfig<DocumentDTO, Long> idColonne = new ColumnConfig<DocumentDTO, Long>(documentProperties.id(), 100, "Id");

		// TYPE
		ColumnConfig<DocumentDTO, String> typeColonne = new ColumnConfig<DocumentDTO, String>(documentProperties.type(), 100, "Type");
		// DATE
		ColumnConfig<DocumentDTO, Date> datelColonne = new ColumnConfig<DocumentDTO, Date>(documentProperties.date(), 100, "Date");
		datelColonne.setCell(new DateCell(DateTimeFormat.getFormat("dd/MM/yyyy")));
		// INTERLOCUTEUR
		ColumnConfig<DocumentDTO, String> interocuteurlColonne = new ColumnConfig<DocumentDTO, String>(documentProperties.interlocuteur(), 150, "Interlocuteur");
		// MONTANT
		ColumnConfig<DocumentDTO, Double> montantColonne = new ColumnConfig<DocumentDTO, Double>(documentProperties.montant(), 100, "Montant");
		montantColonne.setCell(new AbstractCell<Double>() {

			@Override
			public void render(Context context, Double value, SafeHtmlBuilder shb) {
				if (value != null) {
					String style = "style='color: " + (value < 0 ? "red" : "green") + "'";
					String v = numberFormat.format(value);
					shb.appendHtmlConstant("<span " + style + " qtitle='Change' qtip='" + v + "'>" + v + "</span>");
				}
			}
		});

		// JUSTIFICATIF
		/*
		 * ColumnConfig<DocumentDTO, String> justificatiflColonne = new
		 * ColumnConfig<DocumentDTO, String>(documentProperties.filename(), 100,
		 * "Justificatif"); justificatiflColonne.setCell(new
		 * AbstractCell<String>() { // final ImagesCellTemplates icTemplates =
		 * // GWT.create(ImagesCellTemplates.class); final PdfLinkCellTemplates
		 * pdfLCTemplates = GWT.create(PdfLinkCellTemplates.class);
		 * 
		 * @Override public void render(Context context, String value,
		 * SafeHtmlBuilder shb) { // SafeUri imageUri =
		 * UriUtils.fromString(moduleBaseUrl + // "images/logo-pdf.png"); //
		 * shb.append(icTemplates.imageLink(imageUri, value)); if (value !=
		 * null) { shb.append(pdfLCTemplates.pdfLink(value)); }
		 * 
		 * } });
		 */

		List<ColumnConfig<DocumentDTO, ?>> columnsList = new ArrayList<ColumnConfig<DocumentDTO, ?>>();
		columnsList.add(typeColonne);
		columnsList.add(datelColonne);
		columnsList.add(interocuteurlColonne);
		columnsList.add(montantColonne);
		// columnsList.add(justificatiflColonne);
		documentColumnModel = new ColumnModel<DocumentDTO>(columnsList);
	}

	private ListStore<DocumentDTO> getDocumentStore() {
		ListStore<DocumentDTO> documentStore = new ListStore<DocumentDTO>(documentProperties.key());
		documentStore.add(new DocumentDTO("Document 23", "REPAS", new Date(), "New Delhi", 56.8));
		documentStore.add(new DocumentDTO("Document 12", "REPAS", new Date(), "Paris", 75.8));
		return documentStore;
	}

	@Override
	public Widget asWidget() {
		return uiWidget;
	}

	@UiHandler("documentGrid")
	void onCellClick(CellClickEvent event) {
		DocumentDTO documentClicked = documentStore.get(event.getRowIndex());
		presenter.showDetailDocument(documentClicked);
	}

	@UiHandler("newDocumentBtn")
	public void newBoutonClicked(SelectEvent event) {
		presenter.newDocument();
	}

	public DocumentGridView() {
		initializeColumnModel();
		documentStore = getDocumentStore();
		uiWidget = uiBinder.createAndBindUi(this);
	}

	@Override
	public IDocumentGridPresenter getPresenter() {
		return presenter;
	}

	@Override
	public void setPresenter(IDocumentGridPresenter presenter) {
		this.presenter = presenter;
	}

}
